﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JzSport.WebApp.Promotional
{
    public partial class AuditingList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid promotionalActivityId = Guid.Empty;
                if (!Guid.TryParse(Request.QueryString["id"], out promotionalActivityId))
                {
                    Response.Redirect("./Error.aspx");
                }

                int currentUserId = Api.ApiPageUtil.GetCurrentUserId();
                if (currentUserId == 0)
                {
                    Response.Redirect("./Error.aspx");
                }

                ServiceResult<PromotionalActivity> srPActivity = new PromotionalActivityService().Get(promotionalActivityId);
                if (srPActivity.ErrorCode == ErrorCode.NoError && srPActivity.Data != null)
                {
                    _ctlHiddenActivityId.Value = srPActivity.Data.ActivityId.ToString();
                    _ctlHiddenCurrentUserId.Value = currentUserId.ToString();

                    _ctlTextTitle.InnerText = srPActivity.Data.Title;
                    // 验证用户的身份
                    if (new ActivityService().ValidateMember(srPActivity.Data.ActivityId, currentUserId, BasicData.ActivityMemberType.Administrator))
                    {
                        BindList(srPActivity.Data.ActivityId);
                    }
                    else
                    {
                        Response.Redirect("./Error.aspx");
                    }
                }
                else
                {
                    Response.Redirect("./Error.aspx");
                }
            }
        }

        private void BindList(Guid activityId)
        {
            ServiceResult<PagedList<Model.Data.ActivityMember>> srList = new ActivityService().GetRequestJoinUsers(activityId, 0, 9999);
            if (srList.Data != null && srList.Data.Items != null)
            {
                this._ctlList.DataSource = srList.Data.Items;
                this._ctlList.DataBind();
            }
        }

        protected void _ctlList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Guid activityId = new Guid(_ctlHiddenActivityId.Value);
            int currentUserId = Convert.ToInt32(_ctlHiddenCurrentUserId.Value);
            if(string.Equals(e.CommandName, "Approve"))
            {
                int targetUserId = Convert.ToInt32(e.CommandArgument);

                if(new ActivityService().AcceptJoinRequest(targetUserId, activityId).ErrorCode == ErrorCode.NoError)
                {
                    BindList(activityId);
                }
            }
            else if(string.Equals(e.CommandName, "Reject"))
            {
                int targetUserId = Convert.ToInt32(e.CommandArgument);
                if (new ActivityService().RejectJoinRequest(targetUserId, activityId).ErrorCode == ErrorCode.NoError)
                {
                    BindList(activityId);
                }
            }
        }
    }
}